<template>	
	<div>
		<checkbox-group @change="checkchange">
			<view class="cartitem"  v-for="Good in shopscar">
				<checkbox class="GoodSelete" type="checkbox" :value="Good.id" :id="Good.id" :key="Good.id"></checkbox>
			
				<image :src="Good.images" @click="ToGood(Good.id)"  class="ShopcaiItem" ></image>
				<view class="caritemname">{{Good.name}}</view>
				<view class="scartitemold">{{Good.sold}}人已经购买</view>
				<view class="cartitemprice">¥{{Good.price}}
					<view style="float: right;" v-if="Good.dis != 10">(折后¥{{(Good.price * (Good.dis/10)).toFixed(2)}})</view>
				</view>
				<view style="clear: both;"></view>
			</view>
		</checkbox-group>
		
		<view class="buythem" v-if="price != 0">
			合计:¥{{price}}
			<button class="buyth" @click="buythem()">购买</button>
		</view>
		<view style="height: 5vw;margin-top: 10vw;"></view>
		<foot activeBtn = '3'></foot>
		{{checkGood}}
	</div>
</template>

<script>
import foot from "../../component/foot.vue";



	var item = null
	var shopscar = null
	var price = 0.00
	var a = {}
	export default {
		name:'shopscar',
		data() {
			return {
				item:item,
				shopscar:shopscar,
				price:price,
				checkGood:"",
				a:a
			}
		},
		onLoad:function(){
			a = {}
			uni.request({
				url: "http://" + getApp().globalData.IpAddr + "/goods-order/lookcart",
				method:"GET",
				header:{	'authorization':getApp().globalData.token	},
				success:res=> {
					if(!res.data.success)	wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
					else{
						// console.log(res)
						getApp().globalData.goodscart = res.data.data
						this.shopscar = getApp().globalData.goodscart
						this.price = 0.00
						for(var item in this.shopscar) {
							this.price = this.price + Number(this.shopscar[item].price * (this.shopscar[item].dis / 10))
						}
						this.price = this.price.toFixed(2)
					}
				}
			})
		},
		components: {
			"foot": foot,
		},
		methods:{
			checkchange(e){
				// var a = {}
				for(var key in e.detail.value){
					// console.log(e.detail.value[key])
					a[key] = e.detail.value[key]
				}
				console.log(a)
			},
			buythem(){
				// console.log(a)
					uni.request({
						url: "http://" + getApp().globalData.IpAddr + "/goods-order/remove/cart",
						method:"PUT",
						header:{	'authorization':getApp().globalData.token	},
						data: a ,
						success:res=> {
							a = {}
							// console.log(res)
							if(!res.data.success)	wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
							else{
								wx.showToast({	title: "购买成功",	duration: 2000	})
								uni.request({
									url: "http://" + getApp().globalData.IpAddr + "/goods-order/lookcart",
									method:"GET",
									header:{	'authorization':getApp().globalData.token	},
									success:res=> {
										if(!res.data.success)	wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
										else{
											// console.log(res)
											getApp().globalData.goodscart = res.data.data
											this.shopscar = getApp().globalData.goodscart
											this.price = 0
											for(var item in this.shopscar) {
												this.price = this.price + Number(this.shopscar[item].price * (this.shopscar[item].dis / 10))
											}
											this.price.toFixed(2)
										}
									}
								})
							}
						}
					})
		
				a = {}
			},
			ToGood(id){
				getApp().globalData.goodsId = id;
				uni.request({
					url: "http://" + getApp().globalData.IpAddr + "/goods/" + getApp().globalData.goodsId,
					method:"GET",
					header:{	'authorization':getApp().globalData.token	},
					success:res=> {
						getApp().globalData.NowGood = res.data.data
						// console.log(res)
						if(res.data.success) uni.navigateTo({	url: "/pages/ext/commodity"	})
						else wx.showToast({	title: '请求发送失败',	duration: 2000	})
					}
				})
			}
		}
	}
</script>

<style>
	.GoodSelete{
		float: left;
		width: 40px;
		margin-top: 30px;
	}
	.ShopcaiItem{
		width: 90px;
		height: 90px;
		float: left;
	}
	.cartitem{
		/* background-color: red; */
		padding: 5px;
	}
	.caritemname{
		margin-left: 10px;
	}
	.cartitemprice{
		margin-top: 20px;
		margin-left: 20px;
		font-size: 15px;
		float: right;
	}
	.scartitemold{
		margin-left: 30vw;
		margin-top: 1vw;
		font-size: 5px;
	}
	.buythem{
		color: #ff4e4e;
		margin-top: 7vw;
		margin-left: 5vw;
	}
	.buyth{
		float: right;
		margin-right: 8vw;
		background-color: #ffc0c0;
		font-size: 3vw;
		padding-top: 0px;
		width: 20vw;
		border-radius: 20px;
		height: 8vw;
	}
</style>